#include<bits/stdc++.h>
using namespace std;

int S[150010];

int getf(int k) {
    if(S[k]==k) return k;
    return S[k]=getf(S[k]);
}

void merge(int x,int y) {
    S[getf(y)]=getf(x);
}

int main() {
    int n,m;
    cin >> n >> m;
    for (int i = 1;i <= 3*n; ++i) {
        S[i] = i;
    }
    
    int x,y,z;
    int sum=0;
    for (int i = 1; i <= m; ++i) {
        cin >> z >> x >> y;
        if(x > n || y > n) {
            ++sum;
            continue;
        }
        
        if (z == 1) {
            if(getf(x+n) == getf(y) || getf(x + 2 * n) == getf(y)) {
                ++sum;
                continue;
            }
            
            merge(x,y);
            merge(x + n, y + n);
            merge(x + 2 * n, y + 2 * n);
        }
        
        if (z == 2) {
            if (getf(x) == getf(y) || getf(x + 2 * n) == getf(y)) {
                ++sum;
                continue;
            }
            
            merge(x, y + 2 * n);
            merge(x + n, y);
            merge(x + 2 * n, y + n);
        }
    }
    cout << sum << endl;
}
